# 数据载入和可视化
import pandas as pd
import numpy as np

data = pd.read_csv('data_class_raw.csv')
# print(笔记.md.head())
x = data.drop(['y'], axis=1)
y = data.loc[:, 'y']

from matplotlib import pyplot as plt

fig1 = plt.figure(figsize=(5, 5))
bad = plt.scatter(x.loc[:, 'x1'][y == 0], x.loc[:, 'x2'][y == 0])
good = plt.scatter(x.loc[:, 'x1'][y == 1], x.loc[:, 'x2'][y == 1])
plt.legend((good, bad), ('good', 'bad'))
plt.title('质量好坏检测', fontproperties='SimHei', fontsize=20)
plt.xlabel('x1')
plt.ylabel('x2')
# plt.show()

# 步骤1：基于高斯分布概率密度函数，将异常点剔除
from sklearn.covariance import EllipticEnvelope

ad_model = EllipticEnvelope(contamination=0.02)
ad_model.fit(x[y == 0])
y_bad_predict = ad_model.predict(x[y == 0])
# print(y_bad_predict)
# 画出异常点
plt.scatter(x.loc[:, 'x1'][y == 0][y_bad_predict == -1], x.loc[:, 'x2'][y == 0][y_bad_predict == -1], marker='x', s=100)
plt.show()